package com.water.bs.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.water.bs.pojo.OrgApply;
import com.water.bs.pojo.Organization;
import com.water.bs.pojo.User;
import com.water.bs.service.GoodsService;
import com.water.bs.service.OrgManageService;
import com.water.bs.service.UserDonationService;
import com.water.bs.utils.MailUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class OrgManageController {
    @Autowired
    private OrgManageService orgManageService;
    @Autowired
    private GoodsService  goodsService;
    @Autowired
    private MailUtil mailUtil;
    @Autowired
    private UserDonationService userDonationService;

    @RequestMapping("/qryAllOrgApply")
    public JSON qryAllOrgApply(){
        JSONObject rs = new JSONObject();
        List<OrgApply> orgApplies = orgManageService.qryAllOrgApply();
        rs.put("data",orgApplies);
        return rs;
    }

    @RequestMapping("/getOrgApplyPhoto")
    public void getOrgApplyPhoto(@RequestParam("orgid")String orgid, final HttpServletResponse response) throws IOException {
        OrgApply orgApply = orgManageService.getOrgApplyPhoto(orgid);
        byte[] data = orgApply.getPhoto();
        response.setContentType("image/jpeg");
        response.setCharacterEncoding("UTF-8");
        OutputStream outputStream = response.getOutputStream();
        InputStream in = new ByteArrayInputStream(data);
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = in.read(buf, 0, 1024)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();

    }

    @RequestMapping("/agreeOrgApply")
    public JSON agreeOrgApply(@RequestBody JSONObject agreeInfo){
        System.out.println("通过审批的机构id是:"+agreeInfo.get("orgid"));
        JSONObject rs = new JSONObject();
        Map<String,Object> map = agreeInfo;
        int i = orgManageService.agreeOrgApply(map);
        if (i>0){
            int i1 = orgManageService.addOrg(map);
            if (i1>0){
                rs.put("state",0);
                rs.put("message","操作成功!");
                mailUtil.sendOrgApplyEmail("", (String) agreeInfo.get("orgemail"),true);
                return rs;
            }else{
                rs.put("state",1);
                rs.put("message","操作失败!");
                return rs;
            }

        }else{
            rs.put("state",1);
            rs.put("message","操作失败!");
            return rs;
        }


    }

    @RequestMapping("/rejectOrgApply")
    public JSON rejectOrgApply(@RequestBody JSONObject rejectInfo){
        System.out.println("拒绝机构审批:"+rejectInfo);
        JSONObject rs = new JSONObject();
        Map<String,Object> map = rejectInfo;
        int i = orgManageService.rejectOrgApply(map);//拒绝机构申请审批 修改applystate =2 再保存拒绝理由
        if (i>0){
            rs.put("state",0);
            rs.put("message","操作成功!");
            mailUtil.sendOrgApplyEmail((String)rejectInfo.get("rejectMessage"),(String)rejectInfo.get("orgemail"),false);
            return rs;
        }else{
            rs.put("state",1);
            rs.put("message","操作失败!");
            return rs;
        }

    }


    @RequestMapping("/qryOrgList")
    public JSON qryOrgList(){
        JSONObject rs = new JSONObject();
        List<Map<String,Object>> organizations = orgManageService.qryOrgList();
        System.out.println("机构列表:"+organizations);
//格式[{address=福建省福州市晋安区五四北泰禾广场, orgname=善良爱心, city=福建省福州市晋安区, phone=15005947273, id=18b2db22-77c5-4cb4-8f23-3aaf6f56d905, orgmsg=我们是一个很好的机构, point=0, email=694886886@qq.com}]
        for (Map<String, Object> organization : organizations) {
            List<Map<String, Object>> goodsNeed = goodsService.qryGoodsNeed((String) organization.get("id"));
            organization.put("goodsneed",goodsNeed);
        }

        rs.put("data",organizations);
        return  rs;
    }

    @RequestMapping("/delOrg")
    public JSON delOrg(@Param("orgid")String orgid){
        JSONObject rs = new JSONObject();
        System.out.println("要删除的机构是"+orgid);
        int i = orgManageService.delOrg(orgid);
        if (i>0){
            rs.put("state","0");
            rs.put("message","操作成功!");
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","操作失败!");
            return rs;
        }
    }
    @RequestMapping("/allDonation")
    public JSON allDonation(){
        JSONObject rs = new JSONObject();
        List<Map<String, Object>> maps = orgManageService.allDonation();
        for (Map<String, Object> map : maps) {
            List<Map<String, Object>> dodetail = userDonationService.qryMyDonationDetail((String) map.get("doid"));
            map.put("dodetail",dodetail);
        }
        rs.put("data",maps);
        return rs;
    }

}
